package com.bdqn.vueboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.vueboot.config.CustomException;
import com.bdqn.vueboot.dto.BuildingDto;
import com.bdqn.vueboot.entity.*;
import com.bdqn.vueboot.mapper.*;
import com.bdqn.vueboot.service.BuildingService;
import com.bdqn.vueboot.service.StaticService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 楼宇维修(Building)表服务实现类
 *
 * @author makejava
 * @since 2022-12-03 09:09:35
 */
@Service("buildingService")
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    StaticMapper staticMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    MattersMapper mattersMapper;

    @Override
    public IPage pageList(IPage page, BuildingDto building) {
        SysUser sysUser1 =null;

        LambdaQueryWrapper<Building> buildingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingLambdaQueryWrapper.like(building.getBuildingAddress() != null&&building.getBuildingAddress() != "", Building::getBuildingAddress, building.getBuildingAddress());

        if (building.getBuildingContactsName() != null&&building.getBuildingContactsName()!="") {
            try {
                sysUser1 = sysUserMapper.selectOneByUsrName(building.getBuildingContactsName());
                buildingLambdaQueryWrapper.eq(sysUser1 != null, Building::getBuildingContactsid, sysUser1.getUsrId());
            }catch (Exception e){
                throw new CustomException("没有该联系人，请重新输入！");
            }
        }
        buildingLambdaQueryWrapper.ge(building.getBuildingStartdatas()!= null&&building.getBuildingStartdatas()!="",Building::getBuildingStartdata,building.getBuildingStartdatas());
        buildingLambdaQueryWrapper.le(building.getBuildingStartdatas2()!= null&&building.getBuildingStartdatas2()!="",Building::getBuildingStartdata,building.getBuildingStartdatas2());
        buildingLambdaQueryWrapper.eq(building.getBuildingMattersId()!= null&&building.getBuildingMattersId()!=0,Building::getBuildingMattersId,building.getBuildingMattersId());
        IPage iPage = buildingMapper.selectPage(page, buildingLambdaQueryWrapper);
        List<BuildingDto> buildingDtoList = new ArrayList<BuildingDto>();


        for (Object building3 : iPage.getRecords()
        ) {
            Building building5 = (Building) building3;
            BuildingDto dto = new BuildingDto();
            BeanUtils.copyProperties(building5, dto);
            buildingDtoList.add(dto);
        }
        buildingDtoList = buildingDtoList.stream().map((item) -> {
            SysUser sysUser = sysUserMapper.selectById(item.getBuildingContactsid());
            item.setBuildingContactsName(sysUser.getUsrName());
            Department department = departmentMapper.selectById(item.getBuildingDepartment());
            item.setBuildingDepartmentName(department.getDepartmentName());
            Matters matters = mattersMapper.selectById(item.getBuildingMattersId());
            item.setBuildingMattersName(matters.getMattersName());
            Static aStatic = staticMapper.selectById(item.getBuildingStatic());
            item.setBuildingStaticName(aStatic.getStaticName());
            return item;
        }).collect(Collectors.toList());
        iPage.setRecords(buildingDtoList);
        return iPage;
    }

    @Override
    @Transactional
    public int add(BuildingDto building1) {
        Building building = new Building();
        BeanUtils.copyProperties(building1, building);
        building.setBuildingStatic(12L);
        SysUser sysUser = sysUserMapper.selectOneByUsrName(building1.getBuildingContactsName());
        if (sysUser == null) {
            throw new CustomException("没有该联系人，请输入正确的联系人！");
        }
        building.setBuildingContactsid(sysUser.getUsrId());
        building.setBuildingDepartment(sysUser.getDepId());
        building.setBuildingStartdata(LocalDateTime.now());
        building.setBuildingId(null);
        int insert = buildingMapper.insert(building);
        return insert;
    }
}

